<?php
// @formatter:off
/**
 * @file dynamic.conf.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-06-03 03:40:32 +0000 (Tue, 03 Jun 2014) $
 *
 * $Id: dynamic.conf.php 112 2014-06-03 03:40:32Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/includes/dynamic.conf.php $
 */
// @formatter:on

// @formatter:off
//
// Setting default values for globals.
// @{
/**
 * @var WCActiveTagsHolder
 */
$wcActiveTagsHolder		= WCActiveTagsHolder::Instance();
/**
 * @var WCTag
 */
$wcTag				= false;
/**
 * @var WCUsersHolder
 */
$wcUsersHolder                  = WCUsersHolder::Instance();
/**
 * @var WCUserDataHolder
 */
$wcUserDataHolder               = WCUserDataHolder::Instance();
/**
 * @var WCUsersHolder
 */
$wcUsersHolder                  = WCUsersHolder::Instance();
/**
 * @var WCRulesHolder
 */
$wcRulesHolder			= WCRulesHolder::Instance();
/**
 * @var WCCommentsHolder
 */
$wcCommentsHolder		= WCCommentsHolder::Instance();
/**
 * @var string
 */
$wcAction			= false;
/**
 * @var string
 */
$wcSubAction			= false;
/**
 * @var string
 */
$wcForbiddenAction		= false;
/**
 * @var boolean
 */
$wcActionForbidden		= false;
/**
 * @var boolean
 */
$wcMobileActionForbidden	= false;
/**
 * @var string
 */
$wcServicePath			= false;
/**
 * @var WCService
 */
$wcService			= false;
/**
 * @var string
 */
$wcServicePath			= false;
/**
 * @var WCGroup
 */
$wcGroup			= false;
$wcGroupForbidden		= false;
$wcUser				= false;
$wcSubUser			= false;
/**
 * @var WCProfile
 */
$wcProfile			= new WCProfile();
/**
 * @var WCSessions
 */
$wcSession			= WCSessions::Instance();
/**
 * @var WCReadRate
 */
$wcReadRate			= false;
/**
 * @var WCDirectory
 */
$wcDirectory			= false;
/**
 * @var WCComic
 */
$wcComic			= false;
/**
 * @var WCImage
 */
$wcImage			= false;
/**
 * @var WCCollectionsHolder
 */
$wcCollectionsHolder		= WCCollectionsHolder::Instance();
/**
 * @var WCCollection
*/
$wcCollection			= false;
/**
 * @var WCCron
 */
$wcCron				= !defined("__SHELL__") ? null : WCCron::Instance();
/**
 * @var WCCron
*/
$wcToolsManager			= !defined("__SHELL__") ? null : WCToolsManager::Instance();
/**
 * @var WCOptions
*/
$wcOptions			= !defined("__SHELL__") ? null : WCOptions::Instance();
/**
 * @var array
*/
$wcThemeAssigns			= array();
/**
 * @var WCSearch
*/
$wcSearch			= WCSearch::Instance();
/**
 * @var WCLog
*/
$wcLog				= false;
/**
 * @var WCRule
 */
$wcRule				= false;
/**
 * @var
 */
$wcRuleAction			= false;
/**
 * @var Securimage
 */
$wcCaptcha			= false;
/**
 * @var string
 */
$wcSkin				= defined("__MOBILE__") ? $wcDefaults["skin-mobile"] : $wcDefaults["skin"];
// @}
// @formatter:on
foreach($wcCompressors as $key => $value) {
	$pattern = array();
	foreach($value["extensions"] as $ext) {
		$pattern[] = "\.{$ext}$";
	}
	$wcCompressors[$key]["pattern"] = "/(".implode("|", $pattern).")/";
}

//
// Loading current group from URL arguments.
if(isset($_REQUEST["group"])) {
	$wcGroup = WCGroupsHolder::I()->item($_REQUEST["group"]);
} else {
	$wcGroup = WCGroupsHolder::I()->item($wcDefaults["group"]);
}

//
// Loading current user and profile based on current session.
if($wcSession->isLogged()) {
	$wcUser = $wcSession->user();
	$wcProfile->load($wcUser);
} else {
	$wcProfile->load();
}
//
// Loading current action from URL arguments and its permissions.
// @{
if(isset($_REQUEST["action"]) && $_REQUEST["action"]) {
	$wcAction = $_REQUEST["action"];
} else {
	//
	// If no action is specified in URL, a default one is set.
	if(defined("__INDEX__")) {
		$wcAction = $wcSkinDefaults["action"];
	} elseif(defined("__MOBILE__")) {
		$wcAction = $wcSkinDefaults["mobile-action"];
	} else {
		$wcAction = false;
	}
}
//
// Loading current action from URL arguments and its permissions.
// @{
if(defined("__SERVICE__") && isset($_REQUEST["service"]) && $_REQUEST["service"]) {
	$wcServicePath = $_REQUEST["service"];
}
// @}
//
// Loading current comic from URL arguments.
// @{
if(isset($_REQUEST["comic"]) && $wcGroup) {
	$wcComic = WCComicsHolder::I()->item($_REQUEST["comic"]);

	if($wcComic && $wcComic->ok() && $wcComic->group == $wcGroup->id) {
		//
		// Setting current directory.
		$wcDirectory = $wcComic->directory();
	} else {
		$wcComic = false;
	}
}
// @}
//
// Loading current image from URL arguments.
// @{
if(!isset($_REQUEST["comic"]) && isset($_REQUEST["image"]) && $wcGroup) {
	$wcImage = WCImagesHolder::I()->item($_REQUEST["image"]);

	if($wcImage && $wcImage->ok() && $wcImage->group == $wcGroup->id) {
		//
		// Setting current directory.
		$wcDirectory = $wcImage->directory();
	} else {
		$wcImage = false;
	}
}
// @}
//
// Loading current directory from URL arguments.
// If current comic is set, this step is ignored because current directory has to
// be the current comic's directory.
if(!$wcComic && isset($_REQUEST["directory"]) && $wcGroup) {
	$wcDirectory = WCDirectoriesHolder::I()->item($_REQUEST["directory"]);
	if(!$wcDirectory || !$wcDirectory->ok() || $wcDirectory->group != $wcGroup->id) {
		$wcDirectory = false;
	}
}

//
// Loading current collection from URL arguments.
if($wcComic && $wcComic->collection > 0 && $wcGroup) {
	$wcCollection = $wcCollectionsHolder->item($wcComic->collection);
} elseif($wcImage && $wcImage->collection > 0 && $wcGroup) {
	$wcCollection = $wcCollectionsHolder->item($wcImage->collection);
} elseif(isset($_REQUEST["collection"]) && $wcGroup) {
	$wcCollection = $wcCollectionsHolder->item($_REQUEST["collection"]);
}

//
// Loading current tag from URL arguments.
if(isset($_REQUEST["tag"]) && $wcGroup) {
	$wcTag = WCTagsHolder::I()->item($_REQUEST["tag"]);
}

//
// Loading current rule from URL arguments.
if(isset($_REQUEST["rule"]) && $wcGroup) {
	$wcRule = $wcRulesHolder->item($_REQUEST["rule"]);

	if($wcRule && $wcRule->ok()) {
		if(isset($_REQUEST["raction"])) {
			foreach($wcRule->actions() as $action) {
				if($_REQUEST["raction"] == $action->id()) {
					$wcRuleAction = $action;
					break;
				}
			}
		}
	}
}

//
// Loading sub user from URL arguments.
if(isset($_REQUEST["user"]) && $wcGroup) {
	if(!$wcUser || $_REQUEST["user"] != $wcUser->id()) {
		$wcSubUser = $wcUsersHolder->item($_REQUEST["user"]);
	}
}

foreach($wcUserXData as $key => $value) {
	if(is_numeric($key)) {
		$wcUserXData["_names"][] = $value["name"];
	}
}

$wcReadRate = WCReadRate::Instance();

if(!defined("__SHELL__")) {
	require_once "{$wcPaths["includes-directory"]}/dynamic.conf.grants.php";
}
if(!defined("__SHELL__")) {
	require_once "{$wcPaths["includes-directory"]}/dynamic.conf.themes.php";
	require_once "{$wcPaths["includes-directory"]}/dynamic.conf.skin.php";
}
